import React, { useState } from 'react';
import { message } from 'antd';
import LoginForm from '../../components/Login/LoginForm';
import '../../styles/login.css';

interface LoginFormData {
  username: string;
  password: string;
  captcha: string;
  remember: boolean;
}

const Login: React.FC = () => {
  const [loading, setLoading] = useState(false);

  // 模拟登录API调用
  const handleLogin = async (formData: LoginFormData) => {
    setLoading(true);
    
    try {
      // 模拟API调用延迟
      await new Promise(resolve => setTimeout(resolve, 1500));
      
      // 模拟登录验证 - 无需验证，直接登录
      // 登录成功
      message.success('登录成功！');
      
      // 保存登录信息到localStorage（可选）
      if (formData.remember) {
        localStorage.setItem('rememberMe', 'true');
        localStorage.setItem('username', formData.username);
      } else {
        localStorage.removeItem('rememberMe');
        localStorage.removeItem('username');
      }
      
      // 跳转到仪表盘
      window.location.href = '/dashboard';
      
    } catch (error: any) {
      message.error(error.message || '登录失败，请重试');
      throw error; // 抛出错误让表单组件处理
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="login-container">
      <div className="login-card">
        <div className="login-header">
          <div className="login-logo">IBMS</div>
          <h1 className="login-title">智能建筑管理系统</h1>
          <p className="login-subtitle">IBMS Business Edition</p>
        </div>
        
        <LoginForm onSubmit={handleLogin} loading={loading} />
        
        <div className="login-footer">
          <div className="copyright">
            <p>© 2024 智能建筑管理系统</p>
            <p>Powered by React & Ant Design</p>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Login;